package com.lineengine.lineengine.result;


import com.lineengine.lineengine.common.ErrorType;
import com.lineengine.lineengine.context.IContext;
import com.lineengine.lineengine.common.CommonErrorCode;
import com.lineengine.lineengine.common.IErrorCode;

/**
 * <p>
 * 结果默认实现
 * </p>
 *
 * @author wenrong.yu
 * @since 2020-11-08 09:43
 */
public class DefaultResult<T extends IContext<T>> implements IResult<T> {
    protected boolean success;

    protected String code;

    protected String message;

    protected ErrorType errorType;

    public DefaultResult() {
    }

    public DefaultResult(ErrorType errorType, boolean success, String code, String message) {
        this.errorType = errorType;
        this.success = success;
        this.code = code;
        this.message = message;
    }

    public static <E extends IContext<E>> DefaultResult<E> success() {
        return new DefaultResult<>(ErrorType.NONE, true, CommonErrorCode.SUCCESS.getCode(), CommonErrorCode.SUCCESS.getMessage());
    }

    public static <E extends IContext<E>> DefaultResult<E> fail(ErrorType errorType, IErrorCode errorCode) {
        return new DefaultResult<>(errorType, false, errorCode.getCode(), errorCode.getMessage());
    }


    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public boolean isSuccess() {
        return success;
    }

    @Override
    public ErrorType getErrorType() {
        return errorType;
    }
}
